package wzh000;

import java.net.*;
import java.io.*;
import java.lang.Thread;
import com.google.gson.Gson;

import java.util.Scanner;
import com.google.gson.Gson;

/**
 * 
 * @author Petros
 */
public class ListenFromTheRight implements Runnable {

	int port;
	Player me;

	// private final MyPlayer mp;
	// private final object _lock;
	// private final List<object> _results;

	ListenFromTheRight(int port, Player mp) {
		this.port = port;
		this.me = mp;

	}

	public void run() {
		try {
			// Thread.sleep(1000);
			ServerSocket connectionSocket = new ServerSocket(port);
			System.out.println("Waiting for client on port " + port);

			while (true) {

				String input = "";
				Socket dataSocket = connectionSocket.accept();
				System.out.println("Just connected to "
						+ dataSocket.getRemoteSocketAddress());
				BufferedReader in = new BufferedReader(new InputStreamReader(
						dataSocket.getInputStream()));
				PrintWriter out = new PrintWriter(dataSocket.getOutputStream());
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(System.in));

				input = in.readLine();
				while ((input != null)) {
					Gson gson = new Gson();

					ListenRightResponse response = gson.fromJson(input,
							ListenRightResponse.class);

					//if (response.cmd.equals("offer")) {
						// _mp.offerhand(response.num_cards);

						// boolean result = me.sendOfferHandToLeftPlayer();
						// //OfferhandRequest newoffer= new
						// OfferhandRequest("ok");
						//
						// String reply= _mp.PickCard(response.getNumCards());
						// //
						// out.println(reply);
						//
						//
						//
						//
						// //else if card.index out of cards
						// //OfferhandRequest newoffer= new
						// OfferhandRequest("out");
						// // object of string convert it to json and send
						// String json = gson.toJson(newoffer);
						// out.println(json);
						// //class.offerhand(dataSocket.getRemoteSocketAddress());

					//} else 
                                        System.out.println(response.cmd);
                                         if (response.cmd.equals("your_turn")) {
						// check status in the main method
                                            System.out.println("I recieved your turn");
                                            YourTurnResponse yourTurnResponse = new YourTurnResponse("ok");
                                            Gson gsons = new Gson();
                                            String json = gsons.toJson(yourTurnResponse);
                                            out.print(json);
                                            //boolean sent=me.respondToRightPlayer("ok");
                                          
                                               // System.out.println("I sent successfuly");
                                           
                                            
                                            
						boolean result = me.sendYourTurnToLeftPlayer();

						if (result == true) {

						} else {

						}
						// YourTurnRequest turn= new YourTurnRequest("ok");
						// String json = gson.toJson(turn);
						// out.println(json);

					}

					input = null;
				}

				out.flush();
				dataSocket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
